using System;
using System.Collections;
using System.Xml;

namespace ScenarioFramework.Modules.PCM.DataModel.ConfigurationManager.MetaWorkflow
{
	//*********************************************************************
	//
	// XMLMetaWorkflowActivities Class
	//
	// The XMLMetaWorkflowActivities class represents a collection base of XMLMetaWorkflowActivity
	// Its imports methods from WAPI.XmlObjectSet like get set and add
	// XMLMetaWorkflowActivities Has no fields
	//
	//*********************************************************************

	public class XMLMetaWorkflowActivities : bOSdotNET.XML.Data.XMLObjectSet 
	{
		public const string MAIN_NODE = "MetaWorkflowActivities";
		
		/** FIELDS **/

		/** no fields **/

		/** CONSTRUCTORS **/

		/** CONSTRUCTOR Empty **/
		public XMLMetaWorkflowActivities()
		{
			this.Init();
		}

		/** CONSTRUCTOR Width xml parsering**/
		public XMLMetaWorkflowActivities(System.Xml.XmlNode nodeList)
		{
			this.Parse (nodeList);
		}

		/** INSTANCE METHODS **/

		override public void Init()
		{
			this.RemoveAll();
		}

		override public void Parse(System.Xml.XmlNode nodeList)
		{
			this.Init();

			System.Xml.XmlNode node;

			for ( int i=0; i<nodeList.ChildNodes.Count;i++)
			{
				node = nodeList.ChildNodes[i];

				switch ( node.Name) 
				{
					case XMLMetaWorkflowActivity.MAIN_NODE:
						this.Add ( new XMLMetaWorkflowActivity(node));
						break;
				}
			}

		}

		/** Method for return a MwF aCTIVITY from collection **/
		public XMLMetaWorkflowActivity Get(string sName)
		{
			XMLMetaWorkflowActivity activity;

			for ( int i=0; i<this.List.Count; i++)
			{
				activity = (XMLMetaWorkflowActivity)this.List[i];
				if ( activity.Name == sName )
				{
					return activity;
				}
			}

			throw new Exception.MetaWorkflowActivityNotFound(sName + " not found.");
		}

		override public bOSdotNET.XML.XMLNode GetXMLNode()
		{
			bOSdotNET.XML.XMLNode xmlNode = new bOSdotNET.XML.XMLNode(XMLMetaWorkflowActivities.MAIN_NODE);

			for ( int i=0; i<this.Count;i++)
			{
				XMLMetaWorkflowActivity activity = (XMLMetaWorkflowActivity)this.List[i];

				xmlNode.AddNode ( activity.GetXMLNode() );
			}

			return xmlNode;
		}

		/** WAPI Wrappers to call SCENARIO ENGINE **/

		/** no wapi **/

		/** SORTING  FIELDS **/

		public enum SortFields 
		{
			Name,
			Description
		}

		/** SORTING METHODS **/


		public void Sort(SortFields sortField, bool isAscending) 
		{
			switch (sortField) 
			{
				case SortFields.Name:
					InnerList.Sort(new NameComparer());
					break;
				case SortFields.Description:
					InnerList.Sort(new DescriptionComparer());
					break;
				
			}
			if (!isAscending) InnerList.Reverse();
		}


		private sealed class NameComparer : IComparer 
		{
			public int Compare(object x, object y) 
			{
				XMLMetaWorkflowActivity first = (XMLMetaWorkflowActivity) x;
				XMLMetaWorkflowActivity second = (XMLMetaWorkflowActivity) y;
				return first.Name.CompareTo(second.Name);
			}
		}

		private sealed class DescriptionComparer : IComparer 
		{
			public int Compare(object x, object y) 
			{
				XMLMetaWorkflowActivity first = (XMLMetaWorkflowActivity) x;
				XMLMetaWorkflowActivity second = (XMLMetaWorkflowActivity) y;
				return first.Description.CompareTo(second.Description);
			}
		}
	}
}
